//
//  DataSourceModel.m
//  类
//
//  Created by rimi on 2016/12/6.
//  Copyright © 2016年 zhongshuilian. All rights reserved.
//

#import "DataSourceModel.h"

@implementation DataSourceModel

+ (NSArray *)modelArrayFromJsonArray:(NSArray *)array {
    
    NSMutableArray *muArray = [NSMutableArray array];
    
    for (NSDictionary *dict in array) {
        DataSourceModel *model = [[DataSourceModel alloc] init];
        
        [model setValuesForKeysWithDictionary:dict];
        
        // 请求回来的数据，可能和我们想要的数据之间有差别，需要我们在 model 层进行封装改造；
        //        model.sCimg = [NSString stringWithFormat:@"%@选中", model.sCname];
        
        [muArray addObject:model];
    }
    
    return muArray;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
}


+ (NSString *)filePath{
    
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingString:@"/UserInfoa.plist"];
    
    return filePath;
    
}

+ (NSString *)saveData{
    
    NSString *savePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingString:@"/UserInfoa.plist"];
    
    NSLog(@"%@",savePath);
    return savePath;
}

- (void)encodeWithCoder:(NSCoder *)aCoder{
    
    [aCoder encodeObject:self.pic forKey:@"pic"];
    [aCoder encodeObject:self.src forKey:@"src"];
    [aCoder encodeObject:self.url forKey:@"url"];
    
    [aCoder encodeObject:@(self.isClick) forKey:@"isClick"];
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    self = [super init];
    
    if (self) {
        
        self.pic = [aDecoder decodeObjectForKey:@"pic"];
        self.src = [aDecoder decodeObjectForKey:@"src"];
        self.url = [aDecoder decodeObjectForKey:@"url"];
        self.isClick = [[aDecoder decodeObjectForKey:@"isClick"] boolValue];
    }
    return self;
}

@end

@implementation Clickbrowse

+ (instancetype)sharedClickbrowseDataModel{
    
    static Clickbrowse  *model = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        model = [Clickbrowse broweseClick];
    });
    
    return model;
}


+ (instancetype)broweseClick{
    
    Clickbrowse *model = [[Clickbrowse alloc]init];
    NSArray *array= [NSKeyedUnarchiver unarchiveObjectWithFile:[DataSourceModel filePath]];
    
    if (array) {
        model.clickbrowseS = [array mutableCopy];
    }else{
        model.clickbrowseS =[NSMutableArray array];
        
    }
    
    return model;
}

- (void)removeModel:(DataSourceModel *)model{
    
    [self.clickbrowseS enumerateObjectsUsingBlock:^(DataSourceModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if ([obj.src isEqualToString:model.src]) {
            
            [self.clickbrowseS removeObject:obj];
            
            *stop = YES;
        }
    }];
    [self save];
}

- (void)save{
    
    [NSKeyedArchiver archiveRootObject:self.clickbrowseS toFile:[DataSourceModel filePath]];
    
}

@end
